<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      stepsConfig: {
        steps: [
          {
            title: '步骤1',
            Attrs: ['commodityNo','bottleProductId', 'modelNo','modelId',  'clientId']
          },
          {
            title: '光瓶信息',
            Attrs: ['productCategoryId', 'productWeight', 'weightScope', 'productVolume', 'volumeScope', 'volumeRemark',
              'bottleInDiameter', 'bottleOutDiameter','bottleLength','bottleWeight', 'headNeckHeight', 'bottleHeight', 'lengthWeightHeight',
              'productQuestion', 'productAssort', 'deepCategoryList', 'remark']
          },
          {
            title: '配套信息',
            Attrs: ['bottleCapId', 'bottleSprayHeadId', 'bottlePartId', 'centerCoverId','otherPartId']
          },
          {
            title: '图片上传'
          }
        ]
      },
      screenshotUpload: true,
      dataFormConfig: {
        id: {
          hidden: true
        },
        commodityNo: {
          label: '商品编号',
          placeholder: '请输入商品编号'
        },
        bottleProductId: {
          label: '光瓶编号',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择光瓶编号',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        modelId: {
          label: '模具',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择模具',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        clientId: {
          label: '客户名称',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择客户名称',
          list: []
        },
        productCategoryId: {
          label: '产品分类',
          list: [],
          type: 'list',
          placeholder: '请选择产品分类'
        },
        productWeight: {
          label: '产品克数',
          type: 'number',
          max: 1000,
          default: 0
        },
        weightScope: {
          label: '克重误差(±)',
          type: 'String',
          default: '±'
        },
        productVolume: {
          label: '产品容量(ml)',
          type: 'number',
          max: 1000,
          default: 0
        },
        volumeScope: {
          label: '容量误差(±)',
          type: 'String',
          default: '±'
        },
        productImageId: {
          label: '产品图片',
          type: 'uploadImage',
          imageType: 'uploadProductImage'
        },
        productDrawingId: {
          label: '产品图纸',
          type: 'uploadImage',
          imageType: 'uploadProductPaper'
        },
        bottleOutDiameter: {
          label: '瓶口外径(mm)',
          placeholder: '请输入瓶口外径',
          default: 15.6
        },
        bottleHeight: {
          label: '瓶高(mm)',
          type: 'number',
          max: 1000,
          default: 0
        },
        bottleCapId: {
          label: '盖子',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择盖子',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        bottleSprayHeadId: {
          label: '喷头',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择喷头',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        bottlePartId: {
          label: '肩套',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择肩套',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        centerCoverId: {
          label: '中套',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择中套',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        otherPartId: {
          label: '其它配件',
          type: 'list',
          filterable: true,
          clearable: true,
          placeholder: '请选择其它配件',
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewModel
        },
        deepCategoryList: {
          label: '深加工种类',
          placeholder: '请选择深加工种类',
          selectMultiple: true,
          type: 'list',
          clearable: true,
          list: []
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入产品备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    addNewModel() {
      const cb = (newValue) => {
        this.initModelInfoList('modelId')
        this.dataForm.modelId = newValue.id
      }
      this.$parent.openModelInfoDialog(cb)
    },
    async init(id, property, cb) {
      this.dataForm.id = id || 0
      this.initProductCategoryList()
      this.initProductInfoList('bottleProductId')
      this.initDeepCategoryList('deepCategoryList')
      this.initModelInfoList('modelId')
      await  this.initClientInfoList('clientId')

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        stepsConfig: this.stepsConfig,
        dataRule: this.getNeedDataRule('commodityNo','status',  'productWeight', 'productVolume','bottleHeight','bottleOutDiameter', 'productDrawingId'),
        initApi: 'commodityApi/getCommodityInfoInfo',
        saveApi: 'commodityApi/saveCommodityInfo',
        updateApi: 'commodityApi/updateCommodityInfo',
        objectName: 'commodityInfo',
        submitCallback: cb,
        screenshotUploadFlag: true
      })

      this.$nextTick(() => {
        if (!this.R.isNil(property)) {
          Object.keys(property).forEach((key) => {
            if (key.startsWith('clientId')) {
              this.dataForm['clientId'] = property[key].clientId
            }else {
              this.dataForm[key] = property[key]
            }
          })
        }
      })
    }
  }
}
</script>
